package com.tank.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.tank.bean.User;
import com.tank.bean.item.Image;
import com.tank.bean.item.Product;
import com.tank.bean.product.ParentType;
import com.tank.bean.product.SonType;
import com.tank.dao.ItemsManageDao;

@Repository("itemsManageDao")
public class ItemsManageDaoImpl extends TankDaoSupport implements ItemsManageDao{
	
	@SuppressWarnings("unchecked")
	public List<ParentType> queryItemsType() {
		StringBuffer hql = new StringBuffer();
		hql.append("from ParentType");
		return this.getHibernateTemplate().find(hql.toString());
	}

	@SuppressWarnings("unchecked")
	public List<Product> queryPublicItemsMessage() {
		StringBuffer hql = new StringBuffer();
		hql.append("from Product order by pubtime desc");
		return this.getHibernateTemplate().find(hql.toString());
	}

	@SuppressWarnings("unchecked")
	public List<Product> queryPublicItemsMessageByType(int typeMark,String itemsType) {
		StringBuffer hql = new StringBuffer();
		if(typeMark==1){
			hql.append("from Product p where p.ParentType.name='"+itemsType+"' order by p.pubtime desc");
		}else if(typeMark==2){
			hql.append("from Product p where p.SonType.name='"+itemsType+"' order by p.pubtime desc");
		}
		return this.getHibernateTemplate().find(hql.toString());
	}

	public Product querySingleItems(long productId) {
		StringBuffer hql = new StringBuffer();
		hql.append("from Product p where p.id="+productId);
		return (Product) this.getHibernateTemplate().find(hql.toString()).get(0);
	}

	@SuppressWarnings("unchecked")
	public List<User> queryDarenList() {
		StringBuffer hql = new StringBuffer();
		hql.append("from User u order by u.fanCount desc");
		return this.getHibernateTemplate().find(hql.toString());
	}

	@SuppressWarnings("unchecked")
	public List<Product> queryCompleteTransactionForItems() {
		StringBuffer hql = new StringBuffer();
		hql.append("from Product p where p.stauts=2 order by p.pubtime desc");
		return this.getHibernateTemplate().find(hql.toString());
	}
	
	public void uploadItems(Product product){
		 this.getHibernateTemplate().save(product);
	}
	@SuppressWarnings("unchecked")
	public List<Product> queryItemsMessageByUser(long userId) {
		StringBuffer hql = new StringBuffer();
		hql.append("from Product p where author.id="+userId+" order by p.pubtime desc");
		return this.getHibernateTemplate().find(hql.toString());
	}

	public List<SonType> queryItemsSonType(long parentId) {
		StringBuffer hql = new StringBuffer();
		hql.append("from SonType st where st.parentType.id = "+parentId);
		return this.getHibernateTemplate().find(hql.toString());
	}

	public List<Product> queryHotItems() {
		StringBuffer hql = new StringBuffer();
		hql.append("from Product  order by viewCount desc");
		return this.getHibernateTemplate().find(hql.toString());
	}

	public List<Product> queryItems(long userId, int type) {
		StringBuffer hql = new StringBuffer();
		hql.append("from Product p where author.id="+userId);
		switch (type) {
		case 1:hql.append(" and p.parentType.name ='数码家电'");
			break;
		case 2:hql.append(" and p.parentType.name ='服饰'");
			break;
		case 3:hql.append(" and p.parentType.name ='文体'");
			break;
		case 4:hql.append(" and p.parentType.name ='家居'");
			break;
		case 5:hql.append(" and p.parentType.name ='其他'");
			break;
		default:
			break;
		}
		hql.append(" order by p.pubtime desc");
		return this.getHibernateTemplate().find(hql.toString());
	}

	public void updateItem(Product product) {
		getHibernateTemplate().update(product);
	}

}
